<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<%@ page import="com.scalar.chiptrack.operations.dataentry.forms.SortDataEntryForm,
                com.scalar.chiptrack.utils.ConfigManager,
                 com.scalar.chiptrack.adaptor.SortLotBinData,
                 java.util.ArrayList,
                 com.scalar.chiptrack.commons.Constants" %>
<%@ page import="java.util.StringTokenizer"%>

<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-nested.tld" prefix="nested" %>
<%@ taglib uri="/WEB-INF/chiptrack-tags.tld" prefix="chiptrack" %>

<chiptrack:checkLogon sourcePage="/sortDataEntry.do"/>
<%String chipTracContext = "/" + ConfigManager.getChipTracContext();%>
<HTML><HEAD><TITLE>Scalarsoft - ChipTrac</TITLE>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">
<LINK href="<%=chipTracContext%>/jsp/includes/stylesheets/scalarsoft.css" type='text/css' rel='stylesheet'>
<script src="<%=chipTracContext%>/jsp/includes/scripts/functions.js"></script>
<script src="<%=chipTracContext%>/jsp/includes/scripts/validations.js"></script>
<script src="<%=chipTracContext%>/jsp/includes/scripts/sort_dataentry.js"></script>
</HEAD>
<BODY bgColor='#ffffff' leftMargin='0' topMargin='0' marginheight='0' marginwidth='0'>
<script language="javascript">
errMsg = '';


function selectPartNumber()
{
    var form    = document.dieBankBinningInfoForm;
    if ( form.processStepId.value == '0' )
    {
        form.action.value   = 'dieBankBinningInfo';
        form.submit();
        return;
    }
	if( form.locationId != null )
    {
	    form.locationId.value='0';
    }
    form.action.value = "loadLocations";
	form.submit();
}

function selectLocation()
{
    var form = document.dieBankBinningInfoForm;
	if( form.locationId.value == '0' )
    {
        form.processStepId.value = '0'
        form.action.value = "dieBankBinningInfo";
	    form.submit();
	    return ;
    }
	form.action.value = "loadLots";
	form.submit();
}

function selectLotNumber()
{
    var form = document.dieBankBinningInfoForm;

	if( form.lotId.value == '0' )
    {
        form.locationId.value = '0'
        form.action.value   = "loadLocations";
	    form.submit();
    	return ;
    }
    form.action.value   = "loadWafers";
	form.submit();
}

function addMore()
{
    var wafersCount	= parseInt(trim(document.dieBankBinningInfoForm.wafersCount.value));
    if( wafersCount == 25 )
    {
        var pa  = document.getElementById("message");
		pa.innerHTML  = "<li>No. of Wafers cannot be more than 25";
		return;
    }
    var form = document.dieBankBinningInfoForm;
    for (  k = 0; k < wafersCount; k++ )
	{
		form['wafers[' + k + '].yield'].disabled=false;
        form['wafers[' + k + '].binRejected'].disabled=false;
    }
    form.action.value   = "addMoreWafers";
	form.submit();
}


function checkLength(element, size)
{
    if(	element.value.length >= size )
	{
		event.returnValue = false;
	}
}



function finalSave()
{
	errors  = "";
    var pa  = document.getElementById("message");
	var form = document.dieBankBinningInfoForm;
    var wafersCount	= parseInt(trim(document.dieBankBinningInfoForm.wafersCount.value));
    var binCount	= parseInt(trim(document.dieBankBinningInfoForm.goodBinsCount.value));

	for (  k = 0; k < wafersCount; k++ )
	{
		var waferIdCtrl	= form['wafers[' + k + '].waferId'];
		var binRejectedCtrl = form['wafers[' + k + '].binRejected'];
		var waferIdEntered	= true;
		var binInfoEntered  = false;
		//TO VALIDATE WAFER ID
		if( waferIdCtrl.value.trim() != "" )
		{
			if ( checkInteger( waferIdCtrl.value , ' WaferId ' , 'Y'  ) == false )
			{
				pa.innerHTML  = errMsg+" for SlNo " + (k+1);
				waferIdCtrl.select();
				return;
			}
            if( waferIdCtrl.value > 25 )
            {
				pa.innerHTML  = "Wafer Id cannot be greater than 25 for SlNo " + (k+1);
				waferIdCtrl.select();
				return;
            }
            if( waferIdCtrl.value == 0 )
            {
				pa.innerHTML  = "Wafer Id cannot Zero for SlNo " + (k+1);
				waferIdCtrl.select();
				return;
            }
		}
		else
		{
			waferIdEntered	= false;
		}
		//TO VALIDATE BINS
		for ( l = 0; l < binCount; l++ )
		{
			var binCtrl	= form['wafers[' + k + '].binQuantities[' + l + '].binQuantity'];

			if( binCtrl.value.trim() != "" )
			{
				if ( checkInteger( binCtrl.value , ' Bin' + (l+1) , 'Y'  ) == false )
				{
					pa.innerHTML  = errMsg+" for SlNo " + (k+1);
					binCtrl.select();
					return;
				}
				binInfoEntered = true;
			}
		}
		//TO VALIDATE BIN REJECTED FIELD
		if( binRejectedCtrl.value.trim() != "" )
		{
			if ( checkInteger( binRejectedCtrl.value , ' Bin Rejected ' , 'Y'  ) == false )
			{
				pa.innerHTML  = errMsg+" for SlNo " + (k+1);
				binRejectedCtrl.select();
				return;
			}
			if ( parseInt ( binRejectedCtrl.value, 10 ) > 0 )
			{
				binInfoEntered = true;
			}
		}

		if ( waferIdEntered == false && binInfoEntered == true )
		{
			pa.innerHTML  = "Enter Wafer Id for SlNo " + (k+1);
			return;
		}
	}

	//TO CHECK DUPLICATE WAFER IDS
	for ( i = 0; i < wafersCount - 1; i++ )
	{
		var waferIdCtrl1	= form['wafers[' + i + '].waferId'];
		for ( j = i + 1; j < wafersCount; j++ )
		{
			var waferIdCtrl2	= form['wafers[' + j + '].waferId'];
			if ( waferIdCtrl1.value != "" && waferIdCtrl2.value != "" && waferIdCtrl1.value == waferIdCtrl2.value )
			{
				pa.innerHTML = "Wafer Id's of two Wafer's should not be same.";
				waferIdCtrl2.select();
				return;
			}
		}
	}

    var grossDice		= parseInt(trim(form.grossDie.value));
    //TO CHECK BIN TOTALS
    for (  k = 0; k < wafersCount; k++ )
    {
        var binTotal    = 0;
        for ( l = 0; l < binCount; l++ )
		{
			var binCtrl	= form['wafers[' + k + '].binQuantities[' + l + '].binQuantity'];
            if ( binCtrl.value != '' )
            {
                binTotal    = binTotal + parseInt ( binCtrl.value, 10 )
            }
        }
        if ( parseInt (binTotal,10) >  parseFloat(grossDice) )
        {
            pa.innerHTML = "Bin total value can not be greater than Gross Die/Wafer(" + grossDice  + ") for Sl.No " + ( k + 1 );
            return;
        }
    }
	for (  k = 0; k < wafersCount; k++ )
	{
		form['wafers[' + k + '].yield'].disabled=false;
        form['wafers[' + k + '].binRejected'].disabled=false;
    }
    form.action.value = "saveWafers";
	form.submit();
}
function calculateYiled( binRowCount )
{
	var pa	= document.getElementById( "message" );
	errors	= "";
	form		= document.dieBankBinningInfoForm;

	noOfWafers		= parseInt(trim(document.dieBankBinningInfoForm.wafersCount.value));
	goodBinsCount	= parseInt(trim(document.dieBankBinningInfoForm.goodBinsCount.value));

	grossDice		= parseInt(trim(form.grossDie.value));
	goodDieCount	= 0;


		for( var binNo = 0; binNo < goodBinsCount; binNo++ )
		{
			var binCtrl	= form['wafers[' + binRowCount + '].binQuantities[' + binNo + '].binQuantity'];
			if ( binCtrl != null && binCtrl.value != "" &&  !isNaN( binCtrl.value ) )
			{
				goodDieCount =  parseInt( goodDieCount, 10 ) + parseInt( binCtrl.value, 10 );
			}
			else if ( binCtrl != null && binCtrl.value != "" &&  isNaN( binCtrl.value ) )
			{
				errors = "Please Enter Bin_" + ( binNo + 1 ) + " Value in Correctly at Location for Sl.No " + ( binRowCount + 1 );
				//tempBinValue.focus();
				//return;
			}
		}
        if ( parseInt(goodDieCount,10) > parseFloat(grossDice) )
        {
            errors = "Bin total value can not be greater than Gross Die/Wafer(" + grossDice  + ") for Sl.No " + ( binRowCount + 1 );
        }



	if ( errors == '')
	{
		yield = parseInt(goodDieCount, 10) / parseFloat(grossDice);
		var yieldValue = formatYieldIntoDoubleDecimalMode ( yield );
        form['wafers['+ binRowCount +'].yield'].value =yieldValue;
        form['wafers['+ binRowCount +'].binRejected'].value =eval(grossDice-goodDieCount);
	}

	if( errors != null )
	{
		pa.innerHTML = errors;
	}
}
function formatYieldIntoDoubleDecimalMode ( yieldValu )
{
	var yldValue	= yieldValu;
	yldValue		= '' + ( Math.round( yieldValu * 100 ) )/ 100;
	var p1			= yldValue.indexOf('.');

	if ( parseInt ( p1 ) == -1 )
	{
		yldValue += '.00'
	}
	else if ( parseInt ( p1 ) != -1 )
	{
		var p2 = yldValue.substring ( p1, yldValue.length );
		if ( parseInt( p2.length ) < 3 )
		{
			yldValue += '0';
		}
	}
	return yldValue;
}

// -- EnD --

</script>

<!-- Top Header Start-->

<%@ include file="/jsp/includes/header.jsp" %>
<!-- Top Header End-->
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
  <TBODY>
  <TR>
	<TD width='180' valign='top' background='<%=chipTracContext%>/jsp/includes/images/left_red_bg.gif'>
<!-- Side Navigation start -->
<%@ include file="/jsp/operations/dataentryMenu.jsp" %>
<!-- end of the side navigations -->
<TD bgcolor="#ffffff">
<IMG height=9 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=8></TD>
<TD bgcolor="#ffffff"><IMG height=480 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=2></TD>
<TD valign="top" width="100%">

<IMG height=1 src="<%=chipTracContext%>/jsp/includes/images/clear.gif" width=500 border=0>
<BR>
<!-- from here your body starts -->
<html:form name="dieBankBinningInfoForm"  type="com.scalar.chiptrack.operations.dataentry.forms.DieBankBinningInfoForm" action="/dieBankBinningInfo">

<input type="hidden" name="action" value="">
<html:hidden name="dieBankBinningInfoForm" property="goodBinsCount"/>

<table border="0" width="90%" align="center">
<tr><td class="NArialW" colspan="5" align="center">Die Bank Binning Info Entry</td></tr>
<tr><td colspan=5>&nbsp;</td></tr>
<tr><td colspan=5 class='NArialL'><B><FONT id="message" styleClass="NArialL" color='FF0000'></FONT></b></td></tr>
<tr><td colspan=5>
  <chiptrack:info styleClass="NArialL" />
  <chiptrack:errors styleClass="NArialL" />
  <chiptrack:messages type="info" styleClass="NArialL" />
</td></tr>
<logic:equal name="showMessage" value="TRUE">
    <tr><td colspan=5 align='left'><font class='NArialS'color='blue'><b><%=Constants.NO_MODIFY_RIGHTS %></b></font></td></tr>
</logic:equal>

<tr><td>
<table border="0" width="90%" align=left>
<tr><td>
<table border="0" width="100%">
    <tr>
        <logic:present name="partNumbers" scope="request">
            <td width="25%" class="NArialL">Part Number:</td>
            <td width="25%" class="NArialL">
            <html:select name="dieBankBinningInfoForm" property="processStepId" styleClass="txfield" onchange="selectPartNumber()" >
                <html:option value="0">Select Part  Number</html:option>
                <html:options collection="partNumbers" property="value" labelProperty="label"/>
            </html:select>
            </td>
        </logic:present>
        <logic:notPresent name="partNumbers" scope="request">
            <td colspan='2' width="50%">&nbsp;</td>
        </logic:notPresent>
        <logic:present name="vendorLocations" scope="request">
            <td width="25%" class="NArialL">Location:</td>
            <td width="25%" align=left class="NArialL">
            <nested:select property="locationId"  name="dieBankBinningInfoForm" styleClass="txfield" onchange="selectLocation()"  >
                <html:option value="0">Select Location</html:option>
                <html:options collection="vendorLocations" property="value" labelProperty="label"/>
            </nested:select>
            </td>
        </logic:present>
        <logic:notPresent name="vendorLocations" scope="request">
            <td colspan='2' width="50%">&nbsp;</td>
        </logic:notPresent>
    </tr>
    <logic:present name="sortLots" scope="request">
        <tr>
            <td width="20%" class="NArialL">Lot Number:</font></td>
            <td width="25%" class="NArialL">
                <html:select name="dieBankBinningInfoForm" property="lotId"  styleClass="txfield" onchange="selectLotNumber()"  >
                    <html:option value="0">Select Lot  Number</html:option>
                    <html:options collection="sortLots" property="value" labelProperty="label"/>
                </html:select>
            </td>
            <html:hidden property="grossDie"/>
            <td width="20%" class="NArialL">&nbsp;</td>
            <td width="20%" align=left class="NArialL">&nbsp;</td>
        </tr>
    </logic:present>

        <TR>
            <TD >&nbsp;</TD>
        </TR>
        <logic:present property="wafers" name="dieBankBinningInfoForm">
        <tr><td colspan=4 align='left'><font class='NArialS'color='blue'><b>Bins of Wafers released to Assembly are preceded with an Asterisk. Bins of such Wafers not released to Assembly can be put on hold. 
        </b></font></td></tr>
        <TR width="100%">
            <TD COLSPAN="4">
            <bean:define name="dieBankBinningInfoForm" id="dieBankBinningInfoFormTemp" type="com.scalar.chiptrack.operations.dataentry.forms.DieBankBinningInfoForm"/>
                <TABLE border="0" width='100%'>
                    <TR class='NArialL' bgcolor="#993333" align='center'>
                        <TD><font color="#FFFFFF">Sl.No</font></TD>
                        <TD><font color="#FFFFFF">Wafer Id</font></TD>
                        <%
                            for ( int i = 0; i < dieBankBinningInfoFormTemp.getGoodBinsCount(); i++ )
                            {
                        %>
                                <TD><font color="#FFFFFF">Bin<%=i+1%></font></TD>
                        <%
                            }
                        %>

                        <TD><font color="#FFFFFF">Bin Rejected</font></TD>
                        <TD><font color="#FFFFFF">Yield</font></TD>
                        <TD><font color="#FFFFFF">Hold</font></TD>
                    </TR>
                    <% int wafersCount  = 0; %>
                    <nested:iterate property="wafers" indexId="idx" type="com.scalar.chiptrack.operations.dataentry.DieBankBinningInfoWafer" id="dieBankBinninfInfoWafer">
                        <% wafersCount++; %>
                        <TR class='NArialL' align='center' bgcolor="<%=(wafersCount%2 == 0 ? "#DEE1E2" : "#FFFFEE")%>">
                            <TD><%=wafersCount%></TD>
                            <nested:equal property="editable" value="true" >
                                <nested:equal property="newWafer" value="true">
                                    <TD><nested:text property="waferId" styleClass="txfield" size="8" disabled="false"  onkeypress='allowInteger()'/></TD>
                                </nested:equal>
                                <nested:equal property="newWafer" value="false">
                                    <TD><nested:text property="waferId" styleClass="txfield" size="8" disabled="true"/></TD>
                                </nested:equal>
                                <%
                                    String calculateYieldJsFunction = "calculateYiled("+(wafersCount-1)+")";
                                %>
                                <nested:empty property="binsUsedForAssembly">
                                    <nested:iterate property="binQuantities" type="com.scalar.chiptrack.operations.dataentry.dao.BinQuantity">
                                        <TD align="right" width="13%"><nested:text property="binQuantity" styleClass="valuetxfield" size="8" onchange="<%=calculateYieldJsFunction%>" onkeypress='allowInteger()'/></TD>
                                    </nested:iterate>
                                </nested:empty>
                                <nested:notEmpty property="binsUsedForAssembly">
                                    <nested:iterate property="binQuantities" type="com.scalar.chiptrack.operations.dataentry.dao.BinQuantity" indexId="binNo" id="binQuantity">
                                        <%
                                            String binsUsedForAssebly = dieBankBinninfInfoWafer.getBinsUsedForAssembly();
                                            StringTokenizer binsUsedForAssemblyTokenizer = new StringTokenizer(binsUsedForAssebly, "&");
                                        %>
                                        <TD align="right" width="13%">
                                            <%
                                                while( binsUsedForAssemblyTokenizer.hasMoreTokens())
                                                {
                                                    String nextToken = binsUsedForAssemblyTokenizer.nextToken();
                                                    if( nextToken.equals( ""+(binNo.intValue()+1) ) )
                                                    {
                                                        out.print("<font class='NArialL'><font color='blue'><b>*</b></font></font>");
                                                        break;
                                                    }
                                                }
                                            %>
                                            <nested:text property="binQuantity" styleClass="valuetxfield" size="8" disabled="true"/>
                                        </TD>
                                    </nested:iterate>
                                </nested:notEmpty>
                                <TD><nested:text property="binRejected" styleClass="valuetxfield" size="8" onkeypress='allowInteger()' disabled="true" /></TD>
                                <TD><nested:text property="yield" styleClass="valuetxfield" size="8" disabled="true"/></TD>
                                <TD><nested:checkbox property="hold" styleClass="txfield"/></TD>
                            </nested:equal>
                            <nested:equal property="editable" value="false" >
                                <TD><nested:text property="waferId" styleClass="txfield" size="8" disabled="true"/></TD>
                                <nested:iterate property="binQuantities" type="com.scalar.chiptrack.operations.dataentry.dao.BinQuantity">
                                    <TD align="right" width="13%"><nested:text property="binQuantity" styleClass="valuetxfield" size="8" disabled="true"/></TD>
                                </nested:iterate>
                                <TD><nested:text property="binRejected" styleClass="valuetxfield" size="8" disabled="true"/></TD>
                                <TD><nested:text property="yield" styleClass="valuetxfield" size="8" disabled="true"/></TD>
                                <TD><nested:checkbox property="hold" styleClass="txfield" disabled="true"/></TD>
                            </nested:equal>
                        </TR>
                    </nested:iterate>
                </TABLE>
            </TD>
        </TR>
        <input type="hidden" name="wafersCount" value="<%=wafersCount%>">
        <TR>
            <TD COLSPAN="4" align='right'><a href="javascript:addMore();"><img src="<%=chipTracContext%>/jsp/includes/images/addmore.gif" border=0 onmouseover="window.status='Click here to Add Wafer Id';return true" onmouseout="window.status='';return true" title="Click here to Add Wafer Id">
            </a></TD>
        </TR>
        <TR>
            <TD >&nbsp;</TD>
        </TR>
        <TR width="100%">
            <TD COLSPAN="4" ALIGN="CENTER">
                <chiptrack:restrictedLink functionality='<%=Functionality.DATA_ENTRY%>' action='<%=User.USER_ACTION_MODIFY%>' href='javascript:finalSave();' onmouseover="window.status='Click to Save Binning Info';return true" onmouseout="window.status='';return true">
                    <img border="0" src="<%=chipTracContext%>/jsp/includes/images/save.gif" title="Click here to Save Binning Info"/>
                </chiptrack:restrictedLink>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
              <a href="<%=chipTracContext%>/dieBankBinningInfo.do"><img border="0" src="<%=chipTracContext%>/jsp/includes/images/cancel.gif" width="53" height="17"onmouseover="window.status='Click to Cancel Changes';return true" onmouseout="window.status='';return true" title="Click here to Cancel Changes"></a>

            </TD>
        </TR>
    </logic:present>
</table>
</td>
</tr>
<tr><td>

</td></tr>

</table>
</td></tr>

</table>
</html:form>
<BR><BR><BR></TD>
<TD bgcolor="#ffffff"><IMG height=1 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=1></TD>
</TR>
</TABLE>
<!-- Footer starts here -->
<%@ include file="/jsp/includes/footer.jsp" %>
<!-- footer end here -->
</BODY>
</HTML>